package cn.m3a1.creditsystem.controller;

import cn.m3a1.creditsystem.bean.Msg;
import cn.m3a1.creditsystem.bean.User;
import cn.m3a1.creditsystem.service.LoginService;
import cn.m3a1.creditsystem.util.GlobalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author wyj
 * @date 2020/2/19
 * @time 下午5:02
 */
@Controller
public class LoginController {
    @Autowired
    LoginService loginService;
    /**
     * 登录验证，返回json数据
     *
     * @param user
     * @return
     */
    @ResponseBody
    @PostMapping(value = "/login")
    public Msg login(HttpServletRequest request, User user) {
        String username = request.getParameter("username");
        //System.out.println(username+user.getPassword());
        if (GlobalUtils.isEmpty(user.getUsername()) || GlobalUtils.isEmpty(user.getPassword())) {
            return Msg.fail().add("msg", "用户名或密码不能为空");
        }
        List<User> users = loginService.getCheckLogin(user);
        Boolean checkLogin = users.size() == 1;
        if (checkLogin) {
            //如果登录成功，将用户名信息存入session中
            request.getSession().setAttribute("loginUser", users.get(0));
            return Msg.success();
        } else {
            return Msg.fail().add("msg", "用户名或密码错误");
        }

    }

    @GetMapping("/checklogin")
    @ResponseBody
    public Msg checkJump(HttpServletRequest request) {
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return Msg.fail();
        }

        return Msg.success();
    }

    @GetMapping("/loginout")
    public  String loginout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("loginUser");
        return "redirect:/login.html";
    }
    @ResponseBody
    @PostMapping(value = "/resetPass")
    public Msg resetPass(HttpServletRequest request, User user,String newpass,String newpass1) {
        HttpSession session = request.getSession();
        User loginUser = (User) session.getAttribute("loginUser");
        if (loginUser == null) {
            return null;
        }
        if (!newpass.equals(newpass1)){
            return Msg.fail().add("msg", "新密码不一致");
        }
        String username = loginUser.getUsername();
        user.setUsername(loginUser.getUsername());
        System.out.println(username+user.getPassword());
        if (GlobalUtils.isEmpty(user.getUsername()) || GlobalUtils.isEmpty(user.getPassword())) {
            return Msg.fail().add("msg", "用户名或密码不能为空");
        }
        List<User> users = loginService.getCheckLogin(user);
        Boolean checkLogin = users.size() == 1;
        if (checkLogin) {
            user.setPassword(newpass);
            Boolean setpassword = loginService.setpassword(user);
            if (setpassword){
                return Msg.success();
            }else{
                return Msg.fail().add("msg", "更新密码失败");
            }
        } else {
            return Msg.fail().add("msg", "用户名或密码错误");
        }

    }
}
